﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Code.Models
{
    public partial class Option_Site
    {
        /// <summary>
        /// 获取或设置SiteOption的Key
        /// </summary>
        public Option_Site_Key EnumName
        {
            get
            {
                return (Option_Site_Key)System.Enum.Parse(typeof(Option_Site_Key), this.Key);
            }
            set
            {
                this.Key = value.ToString();
            }
        }

        public static bool TryPast(string optionName, out Option_Site_Key option)
        {
            try
            {
                option = (Option_Site_Key)System.Enum.Parse(typeof(Option_Site_Key), optionName);
                return true;
            }
            catch 
            {
                option = 0;
                return false; 
            }
        }

        /// <summary>
        /// 获取SiteOption的Value的字符串
        /// </summary>
        public override string ToString()
        {
            return this.Key;
        }

        /// <summary>
        /// 获取SiteOption的Value的布尔值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public bool ToBool()
        {
            return bool.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的16位整型值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public short ToInt16()
        {
            return short.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的32位整型值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public int ToInt32()
        {
            return int.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的64位整型值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public long ToInt64()
        {
            return long.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的单精度浮点值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public float ToFloat()
        {
            return float.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的双精度浮点值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public double ToDouble()
        {
            return double.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的字符串
        /// </summary>
        public decimal ToDecimal()
        {
            return decimal.Parse(this.Value);
        }

        /// <summary>
        /// 获取SiteOption的Value的日期型值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public DateTime ToDateTime()
        {
            return DateTime.Parse(this.Value);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为布尔值
        /// </summary>
        public bool ToBool(out bool result)
        {
            return bool.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为16位整型数
        /// </summary>
        public bool ToInt16(out short result)
        {
            return short.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为32位整型数
        /// </summary>
        public bool ToInt32(out int result)
        {
            return int.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为64位整型数
        /// </summary>
        public bool ToInt64(out long result)
        {
            return long.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为单精度浮点数
        /// </summary>
        public bool ToFloat(out float result)
        {
            return float.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为双精度浮点数
        /// </summary>
        public bool ToDouble(out double result)
        {
            return double.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为十进制数
        /// </summary>
        public bool ToDecimal(out decimal result)
        {
            return decimal.TryParse(this.Value, out result);
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为日期类型
        /// </summary>
        public bool ToDateTime(out DateTime result)
        {
            return DateTime.TryParse(this.Value, out result);
        }

        public bool ToGuid(out Guid id)
        {
            bool rs = false;
            try
            {
                id = new Guid(this.Value);
                rs = true;
            }
            catch { id = new Guid(); }
            return rs;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为布尔，失败则返指定的值
        /// </summary>
        public bool ToBool(bool _default)
        {
            bool result;
            return bool.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为16位整型数，失败则返指定的值
        /// </summary>
        public short ToInt16(short _default)
        {
            short result;
            return short.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为32位整型数，失败则返指定的值
        /// </summary>
        public int ToInt32(int _default)
        {
            int result;
            return int.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为64位整型数，失败则返指定的值
        /// </summary>
        public long ToInt64(long _default)
        {
            long result;
            return long.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为单精度浮点数，失败则返指定的值
        /// </summary>
        public float ToFloat(float _default)
        {
            float result;
            return float.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为双精度浮点数，失败则返指定的值
        /// </summary>
        public double ToDouble(double _default)
        {
            double result;
            return double.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为十进制数，失败则返指定的值
        /// </summary>
        public decimal ToDecimal(decimal _default)
        {
            decimal result;
            return decimal.TryParse(this.Value, out result) ? result : _default;
        }

        /// <summary>
        /// 尝试将SiteOption的Value的转为日期类型，失败则返指定的值
        /// </summary>
        /// <exception cref="System.FormatException" />
        /// <exception cref="System.ArgumentNullException" />
        public DateTime ToDateTime(DateTime _default)
        {
            DateTime result;
            return DateTime.TryParse(this.Value, out result) ? result : _default;
        }
    }
}
